package cn.iocoder.yudao.module.bpm.controller.admin.formdefinition;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.formdefinition.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.formdefinition.FormDefinitionDO;
import cn.iocoder.yudao.module.bpm.convert.formdefinition.FormDefinitionConvert;
import cn.iocoder.yudao.module.bpm.service.formdefinition.FormDefinitionService;

@Api(tags = "a管理后台 - 表单定义")
@RestController
@RequestMapping("/bpm/form-definition")
@Validated
public class FormDefinitionController {

    @Resource
    private FormDefinitionService formDefinitionService;

    @PostMapping("/create")
    @ApiOperation("创建表单定义")
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:create')")
    public CommonResult<Long> createFormDefinition(@Valid @RequestBody FormDefinitionCreateReqVO createReqVO) {
        return success(formDefinitionService.createFormDefinition(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新表单定义")
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:update')")
    public CommonResult<Boolean> updateFormDefinition(@Valid @RequestBody FormDefinitionUpdateReqVO updateReqVO) {
        formDefinitionService.updateFormDefinition(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除表单定义")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:delete')")
    public CommonResult<Boolean> deleteFormDefinition(@RequestParam("id") Long id) {
        formDefinitionService.deleteFormDefinition(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得表单定义")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:query')")
    public CommonResult<FormDefinitionRespVO> getFormDefinition(@RequestParam("id") Long id) {
        FormDefinitionDO formDefinition = formDefinitionService.getFormDefinition(id);
        return success(FormDefinitionConvert.INSTANCE.convert(formDefinition));
    }

    @GetMapping("/list")
    @ApiOperation("获得表单定义列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:query')")
    public CommonResult<List<FormDefinitionRespVO>> getFormDefinitionList(@RequestParam("ids") Collection<Long> ids) {
        List<FormDefinitionDO> list = formDefinitionService.getFormDefinitionList(ids);
        return success(FormDefinitionConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得表单定义分页")
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:query')")
    public CommonResult<PageResult<FormDefinitionRespVO>> getFormDefinitionPage(@Valid FormDefinitionPageReqVO pageVO) {
        PageResult<FormDefinitionDO> pageResult = formDefinitionService.getFormDefinitionPage(pageVO);
        return success(FormDefinitionConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出表单定义 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:form-definition:export')")
    @OperateLog(type = EXPORT)
    public void exportFormDefinitionExcel(@Valid FormDefinitionExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FormDefinitionDO> list = formDefinitionService.getFormDefinitionList(exportReqVO);
        // 导出 Excel
        List<FormDefinitionExcelVO> datas = FormDefinitionConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "表单定义.xls", "数据", FormDefinitionExcelVO.class, datas);
    }

}
